package com.demo.progressdialogdemo;


import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private final int PROGRESS_DLG = 10001;
    private int progressStatus = 0;
    private ProgressDialog progressDlg = null;
    private ProgressHandler handler = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handler = new ProgressHandler();
        Button btn = (Button) findViewById(R.id.btn);
        btn.setText("开始下载");
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                showDialog(PROGRESS_DLG);
            }
        });
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        if (PROGRESS_DLG == id) {
            progressDlg = new ProgressDialog(MainActivity.this);
            progressDlg.setTitle("下载进度");
            progressDlg.setMessage("正在努力下载中...");
            progressDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDlg.setMax(100);
            progressDlg.setButton("确定", (Message) null);

            return progressDlg;
        }
        return super.onCreateDialog(id);
    }

    @Override
    @Deprecated
    protected void onPrepareDialog(int id, Dialog dialog) {
        // TODO Auto-generated method stub
        super.onPrepareDialog(id, dialog);
        if (PROGRESS_DLG == id) {
            progressStatus = 0;
            DownloadThread down = new DownloadThread();
            down.start();
        }
    }

    class ProgressHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            if (msg.what == 111) {
                progressDlg.setProgress(msg.arg1);
                if (msg.arg1 >= 100)
                    progressDlg.dismiss();
            }
        }
    }

    class DownloadThread extends Thread {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                while (progressStatus <= 100) {
                    Thread.sleep(200);
                    progressStatus++;
                    Message msg = new Message();
                    msg.arg1 = progressStatus;
                    msg.what = 111;
                    handler.sendMessage(msg);
                }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }

    }

}